//
//  KLNewFeatureConfig.m
//  WisdomFamilyDoctor
//
//  Created by kinglian_iOS_000 on 2017/10/17.
//  Copyright © 2017年 kinglian. All rights reserved.
//

#import "KLNewFeatureConfig.h"

@implementation KLNewFeatureConfig

+ (void)config {
    [self configWithComplete:nil];
}

+ (void)configWithComplete:(Complete)complete {
    if ([self isNewFeature]) {
        // 弹出版本欢迎界面
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        KLNewFeatureView *newFeature = [[KLNewFeatureView alloc] initWithFrame:window.bounds complete:^{
            if (complete) {
                complete();
            }
        }];
        
        [window addSubview:newFeature];
    }
}

+ (BOOL)isNewFeature {
    NSString *curVersion = [AppGlobal currentVersion]; // 获取当前的版本号
    
    NSString *kVersionKey = @"__versionNumber__";
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:kVersionKey]; // 获取上一次的版本号
    
    if (!lastVersion) {
        [[NSUserDefaults standardUserDefaults] setObject:curVersion forKey:kVersionKey];
        return YES;
    }
    
    if ([curVersion isEqualToString:lastVersion]) { // 版本号相等, 不显示新特性
        return NO;
    }
    
    // 是新特性、
    [[NSUserDefaults standardUserDefaults] setObject:curVersion forKey:kVersionKey];
    return YES;
}

@end






@interface KLNewFeatureView ()
/** 滚动视图 */
@property (strong, nonatomic) UIScrollView *scrollView;
/** <#属性#> */
@property (nonatomic, copy) Complete complete;
@end

@implementation KLNewFeatureView

- (instancetype)initWithFrame:(CGRect)frame complete:(Complete)complete {
    if (self = [super initWithFrame:frame]) {
        self.complete = complete;
        [self setup];
    }
    
    return self;
}

- (void)setup {
    
    self.scrollView.contentSize = CGSizeMake(kScreenWidth * kImageCount, kScreenHeight);
    [self addSubview:self.scrollView];
    
    for (int i = 0; i < kImageCount; i++) {
        // 图片不要放在Assets.xcassets文件中
        NSString *imageName = nil;
        if (iPhone4) {
            imageName = [NSString stringWithFormat:@"guide-i4-0%d", i];
        }else if (iPhone5) {
            imageName = [NSString stringWithFormat:@"guide-i5-0%d", i];
        }else if (iPhone6) {
            imageName = [NSString stringWithFormat:@"guide-i6-0%d", i];
        }else {
            imageName = [NSString stringWithFormat:@"guide-i6p-0%d", i];
        }
        
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:imagePath]];
        imageView.frame = CGRectMake(i * kScreenWidth, 0, kScreenWidth, kScreenHeight);
       [self.scrollView addSubview:imageView];
        
        if (i == 2) {
//            UIButton *enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
//            enterButton.frame = CGRectMake(0, 0.7 * kScreenHeight, kScreenWidth, kScreenHeight * 0.25);
//            enterButton.tag = 2;
//            [enterButton addTarget:self action:@selector(enterButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//            imageView.userInteractionEnabled = YES;
//            [imageView addSubview:enterButton];
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTap)]];
        }
        
    }
}

- (void)imageViewDidTap {
    [self enterButtonClick:nil];
}

- (void)enterButtonClick:(UIButton *)button {
    [UIView animateWithDuration:1.0 animations:^{
        self.alpha = 0.01;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (self.complete) {
            self.complete();
        }
    }];
}

#pragma mark - Lazy
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        scrollView.userInteractionEnabled = YES;
        scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView = scrollView;
    }
    return _scrollView;
}
@end
